//
//  AddBookColorSelectView.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 03.04.25.
//

import SwiftUI

struct AddBookColorSelectView: View {
    
    @Environment(\.presentationMode) var presentationMode
    
    @State var colors = ["#EF443A","#5E6D85","#2488EB","#58C2DC","#5C2DC4","#00A2E6","#FE017A","#408AE1","#4FDB66","#58C2DC","#6359FA","#188FFF","#E9178B","#94D051","#232631","#FBD117","#F3BA31","#FBA71C","#EB566B","#E84143","#B22235","#FD68A6","#D9D9D9","#686D76","#DB9523","#25C5DF","#32A853","#344564","#31C581","#27CC56"]
    
    
    let columns = [
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible()),
    ]
    
    @Binding var selectedColor: String
    
    var body: some View {
        VStack {
            
            ZStack {
                
                Text("颜色选择")
                    .font(.system(size: 16))
                    .bold()
                    .foregroundColor(Color.koiColorTitle)
                    .padding(.vertical, 11)
                HStack {
                    Button(action: {
                        presentationMode.wrappedValue.dismiss()
                    }) {
                        Image("koi_ic_close")
                    }.padding(.horizontal, 22)
                    Spacer()
                    Button(action: {
                        presentationMode.wrappedValue.dismiss()
                    }) {
                        Text("确定")
                            .font(.system(size: 14))
                            .bold()
                            .foregroundColor(Color.koiColorPrimary)
                    }.padding(.horizontal, 22)
                }
            }.padding(.vertical, 10)
            
            ScrollView {
                LazyVGrid(columns: columns,spacing: 20) {
                    ForEach(colors.indices, id:\.self) { index in
                        let color = colors[index]
                        Button(action:{
                            selectedColor = color
                        }) {
                            ZStack {
                                if selectedColor == color {
                                    Circle()
                                        .stroke(Color(hex: color), lineWidth: 1.5)
                                        .frame(width: 29, height: 29)
                                        .padding()
                                }
                                
                                Color(hex: color)
                                    .frame(width:23, height: 23)
                                    .cornerRadius(23)
                            }.frame(width: 30, height: 30)
                        }
                    }
                }
            }
        }
    }
}

@available(iOS 18.0, *)
#Preview {
    @Previewable @State var selectedColor: String = ""
    AddBookColorSelectView(selectedColor: $selectedColor)
}
